package com.jinxi.algorithm.leetcode;

/**
 * @Autho changqi.wu
 * @Date 路在脚下，使劲踩！
 */
public class MixCode {

    public static void print(int[] bar) {
        for (int i : bar) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }

    public int[] rearrangeBarcodes(int[] barcodes) {
        int length = barcodes.length;
        int size1 = length / 2;
        int size2 = length - size1;
        int[] bar1 = new int[size1];
        int[] bar2 = new int[size2];
        for (int i = 0; i < size1; i++) {
            bar1[i] = barcodes[i];
        }
        for (int i = 0; i < size2; i++) {
            bar2[i] = barcodes[i + size1];
        }

        for (int j = 0; j < size2; j++) {
            bar1 = compare(bar1, bar2[j]);
        }

        return bar1;
    }

    public static void main(String[] args) {
        MixCode m = new MixCode();
        int[] bars = {1,3,2,3,1,2,1};
        int val = 1;
        int[] result = m.compare(bars, val);
        print(result);

        int[] barss = {1,1,1,1,2,2,3,3};
        print(m.rearrangeBarcodes(barss));
    }


    public int[] compare(int[] bars, int val) {
        int[] target = new int[bars.length + 1];
        boolean flag = false;
        for (int i = 0; i < bars.length - 1; i++) {
            int temp1 = bars[i];
            int temp2 = bars[i + 1];
            if (flag) {
                target[i+1] = bars[i];
            } else {
                target[i] = bars[i];

                if (temp1 != val && temp2 != val) {
                    target[i + 1] = val;
                    flag = true;
                }
            }

        }
        if (flag) {
            target[bars.length] = bars[bars.length - 1];
        }else {
            target[bars.length -1 ] = bars[bars.length - 1];
            target[bars.length] = val;
        }
        return target;
    }


}
